package com.atguigu.springboot.service.impl;

import com.atguigu.springboot.entity.Employee;
import com.atguigu.springboot.mapper.EmployeeMapper;
import com.atguigu.springboot.service.IEmployeeService;
import lombok.AllArgsConstructor;
import org.springframework.cache.annotation.*;
import org.springframework.stereotype.Service;

/**
 * @author : wenchao.long
 * @date : Created in 2020/11/26 14:14 周四
 * @description :
 */
@Service
@AllArgsConstructor
@CacheConfig(cacheNames = "employee")
public class EmployeeServiceImpl implements IEmployeeService {
    private final EmployeeMapper employeeMapper;

    @Override
    @Cacheable()
    public Employee getById(Integer id) {
        return employeeMapper.getById(id);
    }

    @Override
    @CachePut(key = "#result.id")
    public Employee update(Employee employee) {
        employeeMapper.update(employee);
        return employee;
    }

    @Override
    public void insert(Employee employee) {
        employeeMapper.insert(employee);
    }

    @Override
    @CacheEvict(key = "#id")
    public void deleteById(Integer id) {
        System.out.println("delete方法被调用" + id);
    }

    @Override
    @Caching(
            cacheable = {
                    @Cacheable(key = "#lastName")
            },
            put = {
                    @CachePut(key = "#result.id"),
                    @CachePut(key = "#result.email")
            }
    )
    public Employee getByName(String lastName) {
        return employeeMapper.getByName(lastName);
    }
}
